package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysConfig;
import com.zzl.alligator.management.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 参数配置表(SysConfig)控制层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 分页查询所有数据
     *
     * @param sysConfig 查询实体
     * @return 所有数据
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    public JsonResult findList(SysConfig sysConfig) {
        startPage();
        List<SysConfig> sysConfigList = sysConfigService.findSysConfigList(sysConfig);
        return JsonResult.success(getDataTable(sysConfigList));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param configId 主键
     * @return 单条数据
     */
    @GetMapping("/{configId}")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    public JsonResult findById(@PathVariable Integer configId) {
        return JsonResult.success(sysConfigService.findSysConfigById(configId));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param configKey 键
     * @return 单条数据
     */
    @GetMapping("/configKey/{configKey}")
    public JsonResult findById(@PathVariable String configKey) {
        return JsonResult.success(sysConfigService.findSysConfigByConfigKey(configKey));
    }

    /**
     * 新增数据
     *
     * @param sysConfig 实体对象
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "新增配置", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody SysConfig sysConfig) {
        return JsonResult.success(this.sysConfigService.insertSysConfig(sysConfig));
    }

    /**
     * 修改数据
     *
     * @param sysConfig 实体对象
     * @return 修改结果
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "修改配置", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody SysConfig sysConfig) {
        return JsonResult.success(this.sysConfigService.updateSysConfig(sysConfig));
    }

    /**
     * 删除数据
     *
     * @param configId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{configId}")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "删除配置", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable Integer configId) {
        SysConfig sysConfig = sysConfigService.findSysConfigById(configId);
        if (sysConfig == null) {
            return JsonResult.success();
        }
        return toAjax( this.sysConfigService.deleteById(sysConfig.getConfigKey(),configId));
    }

    /**
     * 数据导出
     *
     * @param config 查询参数
     * @return 导出结果
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "导出配置", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(SysConfig config) {
        List<SysConfig> list = sysConfigService.findSysConfigList(config);
        ExcelUtils<SysConfig> excelUtils = new ExcelUtils<>(SysConfig.class);
        return excelUtils.exportExcel(list, "参数列表");
    }

    @DeleteMapping("/clearCache")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "清空配置缓存", businessType = BusinessTypeEnum.CLEAR)
    public JsonResult clearCache() {
        sysConfigService.clearCache();
        return JsonResult.success();
    }
}